﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Fittings : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_SportType> fatherDic = new Dictionary<int, Jy123.Model.Public_SportType>();
        private List<Jy123.Model.Public_Fittings> listFittings;
        protected void Page_Load(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Fittings BLLFittings = new Jy123.BLL.Public_Fittings();
            listFittings = BLLFittings.GetModelList(string.Empty);
            BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            List<Model.Public_SportType> listSportType = BLLSportType.GetModelList(string.Empty);
            foreach (Model.Public_SportType sType in listSportType)
            {
                fatherDic.Add(sType.ID, sType);
            }
            Verify("Fittings.aspx");
            if (!Page.IsPostBack)
            {
                LoadInitialValue(listFittings);
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Fittings fittings = e.Item.DataItem as Jy123.Model.Public_Fittings;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = fittings.ID.ToString();
                Label nameLabel = e.Item.FindControl("fittingsNameLabel") as Label;
                nameLabel.Text = fittings.FName;
                Label speedLabel = e.Item.FindControl("fittingsSpeedLabel") as Label;
                speedLabel.Text = fittings.SpeedUp.Value.ToString();
                Label stabilityLabel = e.Item.FindControl("fittingsStabilityLabel") as Label;
                stabilityLabel.Text = fittings.StabilityUp.Value.ToString();
                Label fatherLabel = e.Item.FindControl("fittingsFatherLabel") as Label;
                if (fittings.FatherId.HasValue)
                {
                    fatherLabel.Text = fatherDic[fittings.FatherId.Value].SName;
                }

                Label priceLabel = e.Item.FindControl("priceLabel") as Label;
                if (fittings.Price.HasValue)
                {
                    priceLabel.Text = (Convert.ToInt32(fittings.Price.Value)).ToString();
                }
                Label posXLabel = e.Item.FindControl("posXLabel") as Label;
                posXLabel.Text = fittings.PosX.ToString();
                Label posYLabel = e.Item.FindControl("posYLabel") as Label;
                posYLabel.Text = fittings.PosY.ToString();
                Label displayOrderLabel = e.Item.FindControl("displayOrderLabel") as Label;
                if (fittings.DisplayOrder.HasValue)
                {
                    displayOrderLabel.Text = fittings.DisplayOrder.Value.ToString();
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Fittings BLLFittings = new Jy123.BLL.Public_Fittings();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Fittings fittings = BLLFittings.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                fittingsNameTextBox.Text = fittings.FName;
                fittingsSpeedTextBox.Text = fittings.SpeedUp.Value.ToString();
                fittingsStabilityTextBox.Text = fittings.StabilityUp.Value.ToString();
                if (fittings.Price.HasValue)
                {
                    priceTextBox.Text = (Convert.ToInt32(fittings.Price.Value)).ToString();
                }
                posXTextBox.Text = fittings.PosX.ToString();
                posYTextBox.Text = fittings.PosY.ToString();
                if (fittings.FatherId.HasValue)
                {
                    fatherDropDown.SelectedValue = fittings.FatherId.ToString();
                }
                propsSmallImage.Src = fittings.SmallModel;
                propsBigImage.Src = fittings.Model;
                if (fittings.DisplayOrder.HasValue)
                {
                    displayOrderTextBox.Text = fittings.DisplayOrder.Value.ToString();
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLFittings.Delete(id);
                listFittings = BLLFittings.GetModelList(string.Empty);
                LoadInitialValue(listFittings);
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Fittings BLLFittings = new Jy123.BLL.Public_Fittings();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Fittings fittings = new Jy123.Model.Public_Fittings();
                SetFittingValue(fittings);
                int typeId = BLLFittings.Add(fittings);
                if (fileUploadBig.PostedFile != null && fileUploadSmall.PostedFile != null)
                {
                    fittings.Model = CreateLogo(fileUploadBig.PostedFile, typeId, "Model");
                    fittings.SmallModel = CreateLogo(fileUploadSmall.PostedFile, typeId, "SmallModel");
                }
                fittings.ID = typeId;
                BLLFittings.Update(fittings);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Fittings fittings = BLLFittings.GetModel(id);
                SetFittingValue(fittings);
                if (fileUploadBig.PostedFile != null && fileUploadSmall.PostedFile != null)
                {
                    string strLogo = CreateLogo(fileUploadBig.PostedFile, id, "Model");
                    string strSmallLogo = CreateLogo(fileUploadSmall.PostedFile, id, "SmallModel");

                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        fittings.Model = strLogo;
                    }
                    if (!string.IsNullOrEmpty(strSmallLogo))
                    {
                        fittings.SmallModel = strSmallLogo;
                    }
                }

                BLLFittings.Update(fittings);
            }
            listFittings = BLLFittings.GetModelList(string.Empty);
            LoadInitialValue(listFittings);
            ClearPage();
        }

        private void SetFittingValue(Jy123.Model.Public_Fittings fittings)
        {
            fittings.FName = fittingsNameTextBox.Text;
            if (!string.IsNullOrEmpty(fittingsSpeedTextBox.Text))
            {
                fittings.SpeedUp = int.Parse(fittingsSpeedTextBox.Text);
            }
            if (!string.IsNullOrEmpty(fittingsStabilityTextBox.Text))
            {
                fittings.StabilityUp = int.Parse(fittingsStabilityTextBox.Text);
            }
            if (!string.IsNullOrEmpty(priceTextBox.Text))
            {
                fittings.Price = decimal.Parse(priceTextBox.Text);
            }
            if (!string.IsNullOrEmpty(posXTextBox.Text))
            {
                fittings.PosX = int.Parse(posXTextBox.Text);
            }
            if (!string.IsNullOrEmpty(posYTextBox.Text))
            {
                fittings.PosY = int.Parse(posYTextBox.Text);
            }
            if (int.Parse(fatherDropDown.SelectedValue) > 0)
            {
                fittings.FatherId = int.Parse(fatherDropDown.SelectedValue);
            }
            if (!string.IsNullOrEmpty(displayOrderTextBox.Text))
            {
                fittings.DisplayOrder = int.Parse(displayOrderTextBox.Text);
            }
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            fittingsNameTextBox.Text = string.Empty;
            fittingsSpeedTextBox.Text = string.Empty;
            fittingsStabilityTextBox.Text = string.Empty;
            priceTextBox.Text = string.Empty;
            posXTextBox.Text = string.Empty;
            posYTextBox.Text = string.Empty;
        }

        private void LoadInitialValue(List<Jy123.Model.Public_Fittings> listFittings)
        {
            fittingsRepeater.DataSource = listFittings;
            fittingsRepeater.DataBind();
            fatherDropDown.Items.Clear();
            fittingsRepeater.Visible = listFittings.Count > 0;


            BLL.Public_SportType BLLJType = new Jy123.BLL.Public_SportType();
            List<Model.Public_SportType> listJType = BLLJType.GetModelList("ID = 13 OR ID =14");

            fatherDropDown.DataSource = listJType;
            fatherDropDown.DataTextField = "SName";
            fatherDropDown.DataValueField = "ID";
            fatherDropDown.DataBind();
            fatherDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected string CreateLogo(HttpPostedFile postFile, int fittingsId, string modelType)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateFittingsPath(CreateDir.FITTINGS, fittingsId, logoType, modelType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
